package de.bazinga.rotw.network.message.physics;

import java.util.Vector;

import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.message.type.CertifiedMessage;
import com.captiveimagination.jgn.message.type.PlayerMessage;
import com.captiveimagination.jgn.message.type.TimestampedMessage;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Spatial;

import de.bazinga.rotw.collision.CollidableObjects;
import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.network.Server;
import de.bazinga.rotw.network.message.GameMessage;

public class UpdatePhysicsMessage extends Message implements CertifiedMessage, PlayerMessage, TimestampedMessage, GameMessage {
	
	private Vector<Vector3f> location = null;
	private Vector<Quaternion> rotation = null;
	
	
	
	public void handleMessage(RotwGameState game, Message msg, boolean server) {
		if(!(msg instanceof UpdatePhysicsMessage)) return ;
		
		
		
		if(server) {
			location = new Vector<Vector3f>();
			rotation = new Vector<Quaternion>();
			
			Vector<Spatial> obj = CollidableObjects.getInstance().getObjects("physics");
			if(obj != null) {
				for(Spatial vec:obj) {
					location.add(vec.getLocalTranslation());
					rotation.add(vec.getLocalRotation());
				}
			}
			
			((Server)game.getSyncManager()).getServer().sendToPlayer(this, getPlayerId());
		}else{
			// Apply everything
			Vector<Spatial> obj = CollidableObjects.getInstance().getObjects("physics");
			if(obj != null) {
				for(int i=0;i<obj.size();i++) {
					obj.get(i).setLocalTranslation(
							location.get(i).x, 
							location.get(i).y, 
							location.get(i).z);
					obj.get(i).setLocalRotation(
							rotation.get(i));
				}
			}			
		}
	}



	public void setLocation(Vector<Vector3f> location) {
		this.location = location;
	}



	public Vector<Vector3f> getLocation() {
		return location;
	}



	public void setRotation(Vector<Quaternion> rotation) {
		this.rotation = rotation;
	}



	public Vector<Quaternion> getRotation() {
		return rotation;
	}
}